package com.qifenqian.turbot.module.ui.restaurant

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant.SP_ACCOUNT
import com.qifenqian.turbot.config.Constant.SP_KEY_RESTAURANT_ID
import com.qifenqian.turbot.config.TurbotEnum
import com.qifenqian.turbot.module.adapter.restaurant.TableInfoAdapter
import com.qifenqian.turbot.module.contract.RestaurantContract
import com.qifenqian.turbot.module.entity.response.restaurant.RestaurantTableEntity
import com.qifenqian.turbot.module.presenter.RestaurantPresenter
import com.qifenqian.turbot.util.SpUtils
import kotlinx.android.synthetic.main.activity_restaurant_table.*
import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import org.jetbrains.anko.alert
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2018/12/27 8:59
 * @description 餐桌信息
 * @see
 * @since
 */
class RestaurantTableActivity : BaseActivity<RestaurantPresenter, RestaurantContract.View>(), RestaurantContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, RestaurantTableActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_restaurant_table
    }

    override fun bindPresenter(): RestaurantPresenter? {
        return RestaurantPresenter(this, this)
    }

    private var mTables = mutableListOf<RestaurantTableEntity.Table>()
    private var mTableInfoAdapter: TableInfoAdapter? = null

    private var mCondition: RestaurantTableEntity.BalconyCondition? = null

    override fun initial() {
        mPresenter!!.getTableInfo()
        mTableInfoAdapter = TableInfoAdapter(mTables)
        rv_restaurant_table.layoutManager = LinearLayoutManager(this)
        cb_restaurant_table_people_number.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                et_restaurant_table_people_number.visibility = View.VISIBLE
                cb_restaurant_table_min_amount.isChecked = false
                cb_restaurant_table_service_fee.isChecked = false
            } else {
                et_restaurant_table_people_number.visibility = View.GONE
            }
        }
        cb_restaurant_table_min_amount.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                et_restaurant_table_min_amount.visibility = View.VISIBLE
                cb_restaurant_table_people_number.isChecked = false
                cb_restaurant_table_service_fee.isChecked = false
            } else {
                et_restaurant_table_min_amount.visibility = View.GONE
            }
        }
        cb_restaurant_table_service_fee.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                et_restaurant_table_service_fee.visibility = View.VISIBLE
                cb_restaurant_table_min_amount.isChecked = false
                cb_restaurant_table_people_number.isChecked = false
            } else {
                et_restaurant_table_service_fee.visibility = View.GONE
            }
        }
        rv_restaurant_table.adapter = mTableInfoAdapter
        btn_restaurant_table_add.setOnClickListener {
            mTables.add(RestaurantTableEntity.Table(-1, 0, 0, SpUtils.getInstance().getInt(SP_KEY_RESTAURANT_ID, -1, SP_ACCOUNT), (mTables.size + 1).toString(), TurbotEnum.TYPE_HALL.type))
            mTableInfoAdapter!!.notifyDataSetChanged()
        }
        mTableInfoAdapter!!.setOnItemLongClickListener { adapter, view, position ->
            alert("确定删除吗?") {
                positiveButton("确定") {
                    mPresenter!!.deleteTableInfo(mTables[position].id, position)
                }
                negativeButton("取消") {
                    it.dismiss()
                }
            }.show()
            return@setOnItemLongClickListener true
        }
        mTableInfoAdapter!!.setOnEditPersonNumberListener(object : TableInfoAdapter.OnEditPersonNumberListener {
            override fun onEdit(position: Int, table: RestaurantTableEntity.Table?, tvItemTableInfoPeopleNumber: TextView) {
                showMinPersonInputDialog(position, table, tvItemTableInfoPeopleNumber)
            }
        })
        btn_restaurant_table_next.setOnClickListener {
            saveTableInfo()
        }
    }

    private fun saveTableInfo() {
        val tempList = mutableListOf<RestaurantTableEntity.Table>()
        for (table in mTables) {
            if (table.minNumberOfPeople == 0 && table.maxNumberOfPeople == 0) {
                tempList.add(table)
            }
        }
        if (!tempList.isEmpty()) {
            toast("请完善餐桌信息")
            return
        }
        var condition: RestaurantTableEntity.BalconyCondition? = null
        if (cb_restaurant_table_people_number.isChecked) {
            if (et_restaurant_table_people_number.text.toString().isBlank()) {
                toast("请输入最低起订人数")
                return
            }
            condition = RestaurantTableEntity.BalconyCondition(SpUtils.getInstance().getInt(SP_KEY_RESTAURANT_ID, -1, SP_ACCOUNT), mCondition!!.id, 0, et_restaurant_table_people_number.text.toString().toInt(), 0)
        }

        if (cb_restaurant_table_min_amount.isChecked) {
            if (et_restaurant_table_min_amount.text.toString().isBlank()) {
                toast("请输入最低消费金额")
                return
            }
            condition = RestaurantTableEntity.BalconyCondition(SpUtils.getInstance().getInt(SP_KEY_RESTAURANT_ID, -1, SP_ACCOUNT), mCondition!!.id, (et_restaurant_table_min_amount.text.toString().toDouble() * 100).toInt(), 0, 0)
        }
        if (cb_restaurant_table_service_fee.isChecked) {
            if (et_restaurant_table_service_fee.text.toString().isBlank()) {
                toast("请输入包房服务费")
                return
            }
            condition = RestaurantTableEntity.BalconyCondition(SpUtils.getInstance().getInt(SP_KEY_RESTAURANT_ID, -1, SP_ACCOUNT), mCondition!!.id, 0, 0, (et_restaurant_table_service_fee.text.toString().toDouble() * 100).toInt())
        }
        if (!cb_restaurant_table_people_number.isChecked && !cb_restaurant_table_min_amount.isChecked && !cb_restaurant_table_service_fee.isChecked) {
            condition = RestaurantTableEntity.BalconyCondition(SpUtils.getInstance().getInt(SP_KEY_RESTAURANT_ID, -1, SP_ACCOUNT), mCondition!!.id, 0, 0, 0)
        }
        //准备上传的参数的实体类
        val finalRestaurantTableEntity = RestaurantTableEntity(1, condition!!, SpUtils.getInstance().getInt(SP_KEY_RESTAURANT_ID, -1, SP_ACCOUNT), mTables)
        mPresenter!!.saveTableInfo(finalRestaurantTableEntity)
    }

    private fun showMinPersonInputDialog(position: Int, table: RestaurantTableEntity.Table?, tvItemTableInfoPeopleNumber: TextView) {
        val dialog = Dialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null)
        val etDialogEditContent = view.findViewById<EditText>(R.id.et_dialog_edit_content)
        val btnDialogEditCancel = view.findViewById<TextView>(R.id.btn_dialog_edit_cancel)
        val btnDialogEditOk = view.findViewById<TextView>(R.id.btn_dialog_edit_ok)
        dialog.setContentView(view)
        dialog.show()
        view.tv_dialog_edit_title.text = "最低人数"
        etDialogEditContent.hint = "请输入最低人数"
        btnDialogEditCancel.setOnClickListener { dialog.dismiss() }
        btnDialogEditOk.setOnClickListener {
            val content = etDialogEditContent.text.toString()
            if (content.isBlank()) {
                toast("请输入最低人数")
                return@setOnClickListener
            }
            table!!.minNumberOfPeople = content.toInt()
            showMaxPersonInputDialog(position, table, tvItemTableInfoPeopleNumber)
            dialog.dismiss()
        }
    }

    private fun showMaxPersonInputDialog(position: Int, table: RestaurantTableEntity.Table?, tvItemTableInfoPeopleNumber: TextView) {
        val dialog = Dialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null)
        val tvDialogEditTitle = view.findViewById<TextView>(R.id.tv_dialog_edit_title)
        val etDialogEditContent = view.findViewById<EditText>(R.id.et_dialog_edit_content)
        val btnDialogEditCancel = view.findViewById<TextView>(R.id.btn_dialog_edit_cancel)
        val btnDialogEditOk = view.findViewById<TextView>(R.id.btn_dialog_edit_ok)
        dialog.setContentView(view)
        dialog.show()
        tvDialogEditTitle.text = "最高人数"
        etDialogEditContent.hint = "请输入最高人数"
        btnDialogEditCancel.setOnClickListener { dialog.dismiss() }
        btnDialogEditOk.setOnClickListener {
            val content = etDialogEditContent.text.toString()
            if (content.isBlank()) {
                toast("请输入最高人数")
                return@setOnClickListener
            }
            table!!.maxNumberOfPeople = content.toInt()
            dialog.dismiss()
            mTableInfoAdapter!!.notifyDataSetChanged()
            mTables[position] = table
            tvItemTableInfoPeopleNumber.isClickable = false
        }
    }

    override fun getTableInfoResult(isSuccess: Boolean, code: Int, msg: String?, restaurantTableEntity: RestaurantTableEntity?) {
        if (isSuccess) {
            if (restaurantTableEntity != null) {
                mTables.clear()
                mTables.addAll(restaurantTableEntity.tableList)
                mTableInfoAdapter!!.notifyDataSetChanged()
                mCondition = restaurantTableEntity.balconyCondition
                showView()
            }
        } else {
            toast("加载失败")
            finish()
        }
    }

    private fun showView() {
        if (mCondition!!.minReversePeople == 0) {
            cb_restaurant_table_people_number.isChecked = false
        } else {
            cb_restaurant_table_people_number.isChecked = true
            et_restaurant_table_people_number.setText(mCondition!!.minReversePeople.toString())
        }
        if (mCondition!!.minConsumption == 0) {
            cb_restaurant_table_min_amount.isChecked = false
        } else {
            cb_restaurant_table_min_amount.isChecked = true
            et_restaurant_table_min_amount.setText(mCondition!!.minConsumption.toString())
        }
        if (mCondition!!.serviceCharge == 0) {
            cb_restaurant_table_service_fee.isChecked = false
        } else {
            cb_restaurant_table_service_fee.isChecked = true
            et_restaurant_table_service_fee.setText(mCondition!!.serviceCharge.toString())
        }
    }

    override fun saveTableInfoResult(isSuccess: Boolean, code: Int, msg: String) {
        if (isSuccess) {
            finish()
        }
        toast(msg)
    }

    override fun deleteTableInfoResult(isSuccess: Boolean, code: Int, msg: String, position: Int) {
        if (isSuccess) {
            mTables.removeAt(position)
            mTableInfoAdapter!!.notifyItemRemoved(position)
        }
        toast(msg)
    }
}
